<template>
	<view class="container">
		<view class="form">
			<text class="title">注册</text>
			<input class="input" v-model="nickname" placeholder="请输入昵称（无字符限制）" />
			<input class="input" v-model="email" placeholder="请输入邮箱" type="email" />
			<input class="input" v-model="phone" placeholder="请输入电话号码" type="number" />
			<input class="input" v-model="username" placeholder="请输入用户名" />
			<input class="input" v-model="password" type="password" placeholder="请输入密码" password="true" />
			<input class="input" v-model="confirmPassword" type="password" placeholder="请确认密码" password="true" />
			<button class="btn" type="primary" @click="handleRegister">注册</button>
			<view class="link">
				<text>已有账号？</text>
				<navigator url="/pages/tabBar/tabcompage/login/login">去登录</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				nickname: '',
				email: '',
				phone: '',
				username: '',
				password: '',
				confirmPassword: ''
			}
		},
		methods: {
			async handleRegister() {
				if (!this.nickname.trim()) {
					uni.showToast({
						title: '请输入昵称',
						icon: 'none',
						duration: 1500
					});
					return;
				}
				if (!this.email.trim()) {
					uni.showToast({
						title: '请输入邮箱',
						icon: 'none',
						duration: 1500
					});
					return;
				}
				const emailReg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
				if (!emailReg.test(this.email.trim())) {
					uni.showToast({
						title: '邮箱格式不正确',
						icon: 'none',
						duration: 1500
					});
					return;
				}
				if (!this.phone.trim()) {
					uni.showToast({
						title: '请输入电话号码',
						icon: 'none',
						duration: 1500
					});
					return;
				}
				const phoneReg = /^1[3-9]\d{9}$/;
				if (!phoneReg.test(this.phone.trim())) {
					uni.showToast({
						title: '电话号码格式不正确',
						icon: 'none',
						duration: 1500
					});
					return;
				}
				if (!this.username.trim() || !this.password.trim() || !this.confirmPassword.trim()) {
					uni.showToast({
						title: '请输入完整信息',
						icon: 'none',
						duration: 1500
					});
					return;
				}
				if (this.password !== this.confirmPassword) {
					uni.showToast({
						title: '两次密码不一致',
						icon: 'none',
						duration: 1500
					});
					return;
				}
				if (this.password.length < 6) {
					uni.showToast({
						title: '密码不少于6位',
						icon: 'none',
						duration: 1500
					});
					return;
				}

				uni.showLoading({
					title: '注册中...',
					mask: true
				});

				try {
					const now = new Date();
					const requestData = {
						nickname: this.nickname.trim(),
						email: this.email.trim(),
						phone: this.phone.trim(),
						name: this.username.trim(),
						password: this.password.trim(),
						createTime: now.getTime(),
						createDate: now.toLocaleString(),
						status: 1
					};
					console.log("[前端][注册]请求参数：", requestData);
					const res = await uniCloud.callFunction({
						name: 'addUser',
						data: requestData
					});

					console.log("[前端][注册]返回结果：", res);

					if (res.result && res.result.code === 200) {
						uni.hideLoading();
						uni.showToast({
							title: '注册成功，前往登录',
							icon: 'success',
							duration: 1500
						});
						setTimeout(() => {
							uni.redirectTo({
								url: '/pages/tabBar/tabcompage/login/login'
							});
						}, 1500);
					} else {
						uni.hideLoading();
						uni.showToast({
							title: res.result?.msg || '注册失败',
							icon: 'none',
							duration: 1500
						});
					}
				} catch (err) {
					uni.hideLoading();
					console.error("[前端][注册]异常：", err);
					uni.showToast({
						title: '网络异常，请重试',
						icon: 'none',
						duration: 1500
					});
				}
			}
		}
	}
</script>

<style scoped>
	.container {
		display: flex;
		justify-content: center;
		align-items: center;
		min-height: 100vh;
		background-color: #f5f5f5;
		padding: 40rpx;
		box-sizing: border-box;
	}

	.form {
		width: 100%;
		max-width: 600rpx;
		background: white;
		padding: 60rpx 40rpx;
		border-radius: 20rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
	}

	.title {
		display: block;
		text-align: center;
		font-size: 48rpx;
		font-weight: 600;
		color: #333;
		margin-bottom: 60rpx;
	}

	.input {
		width: 100%;
		height: 80rpx;
		border: 2rpx solid #e0e0e0;
		border-radius: 10rpx;
		padding: 0 20rpx;
		margin-bottom: 40rpx;
		font-size: 28rpx;
		box-sizing: border-box;
	}

	.input:focus {
		border-color: #007AFF;
		outline: none;
	}

	.btn {
		width: 100%;
		height: 80rpx;
		line-height: 80rpx;
		border-radius: 10rpx;
		font-size: 32rpx;
		margin-bottom: 40rpx;
	}

	.link {
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 28rpx;
		color: #666;
	}

	.link navigator {
		color: #007AFF;
		margin-left: 10rpx;
		text-decoration: none;
	}
</style>